//
//  ShopInfoV2Cell.m
//  OrderSeat
//
//  Created by 王威 on 13-12-14.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "ShopInfoV2Cell.h"

@implementation ShopInfoV2Cell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setupUI
{
    int y = 5, x = 5, cellWidth = 300;
    
#define BACK_IMAG_TAG 11
    _backImageView = (UIImageView*)[self.contentView viewWithTag:BACK_IMAG_TAG];
    if (!_backImageView) {
        UIImage *backImage = [[UIImage imageNamed:@"shop_cell_bg.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
        _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, y, 300, 90)];
        _backImageView.image = backImage;
        _backImageView.tag = BACK_IMAG_TAG;
        
        [self.contentView addSubview:_backImageView];
    }
    
    
#define SHOP_NAME_TAG 12
    y = 2;
    _shopNameLabel = (UILabel *)[_backImageView viewWithTag:SHOP_NAME_TAG];
    if (!_shopNameLabel) {
        _shopNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 240, 21)];
        _shopNameLabel.textColor = [UIColor redColor];
        [_shopNameLabel setBackgroundColor:[UIColor clearColor]];
        _shopNameLabel.font = [UIFont boldSystemFontOfSize:16];
        _shopNameLabel.text = @"森林雨鸭肠王火锅（武都路店）";
        _shopNameLabel.tag = SHOP_NAME_TAG;
        
        [_backImageView addSubview:_shopNameLabel];
    }
    
#define ZHEKOU_TAG 13
    x = cellWidth - 5 - 50;
    
    _btnZhekou = (UIButton *)[_backImageView viewWithTag:ZHEKOU_TAG];
    if (!_btnZhekou) {
        _btnZhekou = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnZhekou.frame = CGRectMake(x, y, 50, 20);
        [_btnZhekou setBackgroundImage:[UIImage imageNamed:@"zk.png"] forState:UIControlStateNormal];
        [_btnZhekou setTitle:@"9.5 折" forState:UIControlStateNormal];
        _btnZhekou.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [_btnZhekou setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        _btnZhekou.tag = ZHEKOU_TAG;
        
        [_backImageView addSubview:_btnZhekou];
    }
    
#define CAT_TAG 14
    
    y += _shopNameLabel.bottom_5;
    _catLabel = (UILabel *)[_backImageView viewWithTag:CAT_TAG];
    if (!_catLabel) {
        _catLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, y, 189, 21)];
        _catLabel.textColor = [UIColor lightGrayColor];
        [_catLabel setBackgroundColor:[UIColor clearColor]];
        _catLabel.font = [UIFont boldSystemFontOfSize:14];
        _catLabel.text = @"拢菜 川菜 家常菜 火锅";
        _catLabel.tag = CAT_TAG;
        
        [_backImageView addSubview:_catLabel];
    }
    
#define PRICE_TAG 15
    
    _priceLabel = (UILabel *)[_backImageView viewWithTag:PRICE_TAG];
    if (!_priceLabel) {
        NSString *price = @"￥100";
        CGSize priceSize = [price sizeWithFont:[UIFont boldSystemFontOfSize:14]];
        x = cellWidth - priceSize.width-2;
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, priceSize.width, 21)];
        _priceLabel.textColor = [UIColor redColor];
        [_priceLabel setBackgroundColor:[UIColor clearColor]];
        _priceLabel.font = [UIFont boldSystemFontOfSize:14];
        _priceLabel.text = price;
        _priceLabel.tag = PRICE_TAG;
        
        [_backImageView addSubview:_priceLabel];
    }
    
#define PRICE_TITLE_TAG 16
    _priceTitleLabel = (UILabel*)[_backImageView viewWithTag:PRICE_TITLE_TAG];
    if (!_priceTitleLabel) {
        NSString *priceTitle = @"人均:";
        CGSize priceTitleSize = [priceTitle sizeWithFont:[UIFont systemFontOfSize:14]];
        x = _priceLabel.left - priceTitleSize.width-2;
        
        _priceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, priceTitleSize.width, 21)];
        _priceTitleLabel.textColor = [UIColor grayColor];
        [_priceTitleLabel setBackgroundColor:[UIColor clearColor]];
        _priceTitleLabel.font = [UIFont systemFontOfSize:14];
        _priceTitleLabel.text = priceTitle;
        
        [_backImageView addSubview:_priceTitleLabel];
    }
    
#define LINE_TAG 17
    
    _line = (UIImageView*)[_backImageView viewWithTag:LINE_TAG];
    if (!_line) {
        y = _catLabel.bottom_5;
        _line = [[UIImageView alloc] initWithFrame:CGRectMake(0, y, cellWidth, 2)];
        _line.image = [UIImage imageNamed:@"xuxian.png"];
        
        [_backImageView addSubview:_line];
    }
    
#define MAP_TAG 18
    _dituImageView = (UIImageView*)[_backImageView viewWithTag:MAP_TAG];
    if (!_dituImageView) {
        y = _line.bottom_5;
        _dituImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, y+5, 10, 10)];
        _dituImageView.image = [UIImage imageNamed:@"ditu.png"];
        
        [_backImageView addSubview:_dituImageView];
    }
    
#define ADDRESS_TAG 19
    
    _addressLabel = (UILabel*)[_backImageView viewWithTag:ADDRESS_TAG];
    if (!_addressLabel) {
        NSString *address = @"城东区武都路京城剧院旁";
        x = _dituImageView.right;
        CGSize addressSize = [address sizeWithFont:[UIFont systemFontOfSize:12]];
        
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, addressSize.width, 21)];
        _addressLabel.font = [UIFont systemFontOfSize:12];
        [_addressLabel setBackgroundColor:[UIColor clearColor]];
        _addressLabel.text = address;
        _addressLabel.tag = ADDRESS_TAG;
        
        [_backImageView addSubview:_addressLabel];
    }
    
#define DISTANCE_TAG 20
    _distanceLabel = (UILabel*)[_backImageView viewWithTag:DISTANCE_TAG];
    if (!_distanceLabel) {
        NSString *distance = @"1200.46 KM";
        CGSize distanceSize = [distance sizeWithFont:[UIFont systemFontOfSize:12]];
        x = cellWidth - distanceSize.width-2;
        _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, distanceSize.width, 21)];
        [_distanceLabel setBackgroundColor:[UIColor clearColor]];
        _distanceLabel.font = [UIFont systemFontOfSize:12];
        _distanceLabel.text = distance;
        _distanceLabel.tag = DISTANCE_TAG;
        
        [_backImageView addSubview:_distanceLabel];
    }
}

- (void)setContent:(NSDictionary*)dic
{
    //    _backImageView = (UIImageView*)[self.contentView viewWithTag:BACK_IMAG_TAG];
    
    NSString *shopName = [dic objectForKey:@"name"];
    //    _shopNameLabel = (UILabel *)[_backImageView viewWithTag:SHOP_NAME_TAG];
    _shopNameLabel.text = shopName;
    
    NSString *zhekou = [dic objectForKey:@"zhekou"];
    //    _btnZhekou = (UIButton *)[_backImageView viewWithTag:ZHEKOU_TAG];
    if ([zhekou isEqualToString:@"0"]) {
        _btnZhekou.hidden = YES;
    }else {
        _btnZhekou.hidden = NO;
        [_btnZhekou setTitle:zhekou forState:UIControlStateNormal];
    }
    
    NSString *caixi = [dic objectForKey:@"caixi"];
    //    _catLabel = (UILabel *)[_backImageView viewWithTag:CAT_TAG];
    _catLabel.text = caixi;
    
    NSString *price = [dic objectForKey:@"renjun"];
    //    _priceLabel = (UILabel *)[_backImageView viewWithTag:PRICE_TAG];
    _priceLabel.text = price;
    
    NSString *address = [dic objectForKey:@"xianqu"];
    //    _addressLabel = (UILabel*)[_backImageView viewWithTag:ADDRESS_TAG];;
    _addressLabel.text = address;
    
    NSString *distance = @"1200.46 KM";
    //    NSString *jingdu = [dic objectForKey:@"jingdu"];
    //    NSString *weidu = [dic objectForKey:@"weidu"];
    //    _distanceLabel = (UILabel*)[_backImageView viewWithTag:DISTANCE_TAG];
    _distanceLabel.text = distance;
    
    [self adjustSubview:dic];
}

- (void)adjustSubview:(NSDictionary *)dic
{
    int cellWidth = 300;
    
    //    _backImageView = (UIImageView*)[self.contentView viewWithTag:BACK_IMAG_TAG];
    
    //    _catLabel = (UILabel *)[_backImageView viewWithTag:CAT_TAG];
    
    NSString *price = [dic objectForKey:@"renjun"];
    //    _priceLabel = (UILabel *)[_backImageView viewWithTag:PRICE_TAG];
    CGSize priceSize = [price sizeWithFont:[UIFont boldSystemFontOfSize:14]];
    
    _priceLabel.left = cellWidth - priceSize.width-2;
    _priceLabel.width = priceSize.width;
    
    //    _priceTitleLabel = (UILabel*)[_backImageView viewWithTag:PRICE_TITLE_TAG];
    NSString *priceTitle = @"人均:";
    CGSize priceTitleSize = [priceTitle sizeWithFont:[UIFont systemFontOfSize:14]];
    _priceTitleLabel.left = _priceLabel.left - priceTitleSize.width-2;
    _priceTitleLabel.width = priceTitleSize.width;
    
    //    _line = (UIImageView*)[_backImageView viewWithTag:LINE_TAG];
    _line.top = _catLabel.bottom_5;
    
    //    _dituImageView = (UIImageView*)[_backImageView viewWithTag:MAP_TAG];
    _dituImageView.left = _catLabel.left;
    _dituImageView.top = _line.bottom_10;
    
    NSString *address = [dic objectForKey:@"xianqu"];
    _addressLabel = (UILabel*)[_backImageView viewWithTag:ADDRESS_TAG];
    CGSize addressSize = [address sizeWithFont:[UIFont systemFontOfSize:12]];
    _addressLabel.left = _dituImageView.right_5;
    _addressLabel.width = addressSize.width > 200 ? 200 : addressSize.width;
    
    //    _distanceLabel = (UILabel*)[_backImageView viewWithTag:DISTANCE_TAG];
    NSString *distance = @"1200.46 KM";
    CGSize distanceSize = [distance sizeWithFont:[UIFont systemFontOfSize:12]];
    _distanceLabel.left = cellWidth - distanceSize.width - 2;
    _distanceLabel.width = distanceSize.width;
}


@end
